#############################################
#Covariate balance table
#############################################

rm(list = ls())

#Functions
source("functions.R")

#Packages
installPackageNotFound("xtable")

#############################################
#Read in Voter data
#############################################

#All voters
all.voters <- read.csv("voters.csv", stringsAsFactors = F)

#Drop voter pairs in same household as volunteer
all.voters <- all.voters[which(all.voters$same_household == 0),]

#############################################
#Run balance tests within each sample of interest
#############################################

#Voter controls
covariates <- c("Black", "Hispanic", "Asian", "Male", "Under30",
                "VotePropensity", "Partisanship")

#Data groups
data.list <- list("in.district" = which(all.voters$Volunteer_In_District == 1),
                  "out.district" = which(all.voters$Volunteer_In_District == 0))

#Object to hold results
result.df <- c()

for(k in 1:length(data.list)){

  #Select data
  test.data <- all.voters[data.list[[k]],]

  #Run tests
  this.result <-runTTests(test.data = test.data,
                        outcome.var.names = covariates, indp.var = "Treatment")

  #Label sample
  this.result$sample <- names(data.list)[k]

  #Add to results
  result.df <- rbind(result.df,this.result)
}

#Separate means and ses
mean.df <- result.df[, c("sample", "outcome", "control_mean", "treatment_mean",
                        "bh_p_value")]

#Format
mean.df$control_mean <- formatSig(mean.df$control_mean, 2)
mean.df$treatment_mean <- formatSig(mean.df$treatment_mean, 2)
mean.df$bh_p_value <- formatSig(mean.df$bh_p_value, 2)
mean.df$type <- 1

#Insert ses underneath means
se.df <- result.df[,c("sample", "outcome", "control_se", "treatment_se",
                      "bh_p_value")]

#Format
se.df$control_se <- paste0("(",formatSig(se.df$control_se,2), ")")
se.df$treatment_se <- paste0("(", formatSig(se.df$treatment_se,2), ")")
se.df$bh_p_value <- ""
se.df$type <- 2

#Rename
names(se.df) <- names(mean.df)

#Bind together
out.table <- rbind(mean.df, se.df)

#Sort
out.table <- out.table[order(out.table$sample, out.table$outcome),]

#Blank out the variable
out.table$outcome[which(out.table$type==2)] <- ""

#Widen
out.table.wide <- cbind(out.table[which(out.table$sample=="in.district"),
                  c("outcome", "control_mean", "treatment_mean", "bh_p_value")],
        out.table[which(out.table$sample=="out.district"),
                  c("control_mean", "treatment_mean", "bh_p_value")])

#Rename
names(out.table.wide)[1] <- "Covariate"

#Get rid of row names
row.names(out.table.wide) <- c()
print(out.table.wide)
